package com.iflytek.universitymanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iflytek.universitymanagement.config.Result;
import com.iflytek.universitymanagement.domain.ScoreAnalysisResult;
import com.iflytek.universitymanagement.domain.StudentGrowth;
import com.iflytek.universitymanagement.domain.StudyHabit;
import com.iflytek.universitymanagement.domain.WeakCourse;
import com.iflytek.universitymanagement.service.StudentGrowthService;
import com.iflytek.universitymanagement.service.StudentScoreService;
import com.iflytek.universitymanagement.service.StudentService;
import com.iflytek.universitymanagement.service.StudyHabitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7000")
@RestController
@RequestMapping("/api/analysis")
public class LearningHabitController {

    @Autowired
    private StudyHabitService studyHabitService;

    @Autowired
    private StudentScoreService studentScoreService;

    @Autowired
    private StudentGrowthService studentGrowthService;

    @Autowired
    private StudentService studentService;

    // 根据学生名字获取学生ID
    private Integer getStudentIdByName(String studentName) {
        return studentService.getStudentIdByName(studentName);
    }

    // 学生学习习惯分析
    @GetMapping("/habits/{studentName}")
    public List<StudyHabit> getStudentHabits(@PathVariable String studentName) {
        Integer studentId = getStudentIdByName(studentName);
        return studyHabitService.getHabitsByStudentId(studentId);
    }

    // 学生成绩分析接口
    @GetMapping("/scores/analysis/{studentName}")
    public ScoreAnalysisResult getScoreAnalysis(@PathVariable String studentName) {
        Integer studentId = getStudentIdByName(studentName);
        return studentScoreService.getScoreAnalysisByStudentId(studentId);
    }


    @GetMapping("/growth/{studentName}")
    public StudentGrowth getStudentGrowth(@PathVariable String studentName) {
        Integer studentId = getStudentIdByName(studentName); // 根据学生姓名获取学生ID
        return studentGrowthService.getGrowthByStudentId(studentId); // 获取学生成长数据
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "10") int pageSize,
                             @RequestParam(required = false) String studentName) {
        IPage<StudyHabit> page = studyHabitService.selectPage(pageNum, pageSize, studentName);
        return Result.success(page);
    }


    @PostMapping("/add")
    public com.iflytek.universitymanagement.config.Result add(@RequestBody StudyHabit studyHabit) {
        studyHabitService.save(studyHabit);
        return com.iflytek.universitymanagement.config.Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public com.iflytek.universitymanagement.config.Result updateById(@RequestBody StudyHabit studyHabit) {
        studyHabitService.updateById(studyHabit);
        return com.iflytek.universitymanagement.config.Result.success();
    }



}
